package chat;


import chat.bean.Message;
import chat.constant.CommonConst;
import chat.constant.MessageType;
import chat.util.MsgUtils;
import chat.util.ScannerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Optional;

@Slf4j
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        //链接服务器
        socket.connect(new InetSocketAddress(CommonConst.PORT));
        String name = "";
        while (true) {
            //登录
            if (StringUtils.isBlank(name)) {
                name = login(socket);
                continue;
            }
            //聊天
            printfMenu();
            int type = ScannerUtils.getInt();
            switch (type) {
                case MessageType.TO_SERVER:
                    sendMsgToServer(socket, name);
                    break;
                case MessageType.TO_FRIEND:
                    sendMsgToFriend(socket, name);
                    break;
                case MessageType.TO_ALL:
                    sendMsgToAll(socket, name);
                    break;
                case MessageType.RECEIVE:
                    receiveMsg(socket, name);
                    break;
                default:
                    break;
            }
        }


    }

    private static void sendMsgToAll(Socket socket, String name) {
        String content;
        do {
            //消息内容
            System.out.print(name + ":");
            content = ScannerUtils.getString();
            Message message = MsgUtils.createMsg(MessageType.TO_ALL, content, name);
            try {
                MsgUtils.writeMsg(socket.getOutputStream(), message);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } while (!StringUtils.equals(content, CommonConst.EXIT));
    }

    private static void receiveMsg(Socket socket, String name) {
        while (true) {
            try {
                Optional<Message> opMsg = MsgUtils.readMsg(socket.getInputStream());
                if (opMsg.isPresent()) {
                    Message msg = opMsg.get();
                    String content1 = msg.getContent();
                    String username = msg.getUsername();
                    System.out.println(username + ":" + content1);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 给指定好友发消息
     *
     * @param socket
     * @param name
     */
    private static void sendMsgToFriend(Socket socket, String name) {
        String content;
        do {
            //好友名字
            System.out.print("请输入好友名字：");
            String friendName = ScannerUtils.getString();
            //消息内容
            System.out.print(name + ":");
            content = ScannerUtils.getString();
            Message message = MsgUtils.createFriendMsg(MessageType.TO_FRIEND, content, name, friendName);
            try {
                MsgUtils.writeMsg(socket.getOutputStream(), message);
                Optional<Message> opMsg = MsgUtils.readMsg(socket.getInputStream());
                if (opMsg.isPresent()) {
                    Message msg = opMsg.get();
                    String content1 = msg.getContent();
                    String username = msg.getUsername();
                    if (CommonConst.USER_NOT_FOUND.equals(content1)) {
                        System.out.println(content1);
                        continue;
                    }
                    System.out.println(username + ":" + content1);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } while (!StringUtils.equals(content, CommonConst.EXIT));

    }

    private static void printfMenu() {
        System.out.println(MessageType.TO_SERVER + ".发送给服务器\n" +
                MessageType.TO_FRIEND + ".发送给指定好友\n" +
                MessageType.TO_ALL + ".群发\n" +
                MessageType.RECEIVE + ".接收信息");
        System.out.print("选择功能：");
    }

    private static String login(Socket socket) throws IOException {
        System.out.println("请先登录！");
        System.out.print("请输入用户名：");
        String username = ScannerUtils.getString();
        System.out.print("请输入密码：");
        String password = ScannerUtils.getString();
        Message message = MsgUtils.createMsg(MessageType.LOGIN, CommonConst.BLACK, username, password);
        MsgUtils.writeMsg(socket.getOutputStream(), message);
        Optional<Message> opMsg = MsgUtils.readMsg(socket.getInputStream());
        if (opMsg.isPresent()) {
            Message msg = opMsg.get();
            String content = msg.getContent();
            if (CommonConst.SUCCESS.equals(content)) {
                System.out.println(username + CommonConst.SUCCESS);
                return username;
            }
            System.out.println(username + CommonConst.FAIL);
        }
        return "";
    }

    private static void sendMsgToServer(Socket socket, String name) throws IOException {
        String content = "";
        do {
            System.out.print(name + ":");
            content = ScannerUtils.getString();
            Message message = MsgUtils.createMsg(MessageType.TO_SERVER, content, name);
            MsgUtils.writeMsg(socket.getOutputStream(), message);
            Optional<Message> opMsg = MsgUtils.readMsg(socket.getInputStream());
            if (opMsg.isPresent()) {
                Message msg = opMsg.get();
                System.out.println(msg.getUsername() + ":" + msg.getContent());
            }
        } while (!StringUtils.equals(content, CommonConst.EXIT));
    }
}
